package movie.mb;

import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;
import movie.ejb.EjbUser;
import movie.entity.UserAccess;
import movie.utility.Constants;
import movie.utility.JsfUtil;
import movie.utility.Utility;

@ManagedBean
@SessionScoped
public class MbUser implements Serializable {

    @EJB
    private EjbUser ejbUser;
    @ManagedProperty(value = "#{mbLogin}")
    private MbLogin mbLogin;
    private UserAccess user;
    private List<UserAccess> listUser;
    private SelectItem[] activeOption;

    public MbUser() {
        System.out.print("Constructor: " + getClass().getName());
    }

    @PostConstruct
    public void postConstruct() {
        System.out.print("Post construct: " + getClass().getName());
        System.out.print(mbLogin);
        activeOption = createFilterOptions();
    }

    public void prepareUserForEdit(UserAccess logedUser) {
        user = logedUser;
        System.out.println("user-update");
        user.setTransPasswordNew1("");
        user.setTransPasswordNew2("");
    }

    public void prepareUserForDelete(UserAccess user) {
        this.user = user;
    }

    public void prepareUserForAdd() {

        user = new UserAccess(0);
        user.setActive(false);
    }

    public boolean addUser() {
        if (!ejbUser.checkUsernameAvailable(user.getUserName())) {
            JsfUtil.addValidationFailedForComponent("editForm:username", "this user exists");
            JsfUtil.failValidation();
            return false;
        } else {
            if (!Utility.isStringEmptyOrNull(user.getTransPasswordNew1())
                    && !Utility.isStringEmptyOrNull((user.getTransPasswordNew2()))) {
                if (!user.getTransPasswordNew1().equals(user.getTransPasswordNew2())) {
                    JsfUtil.addValidationFailedForComponent("editForm:new2password", "passwords is diffrent");
                    JsfUtil.failValidation();
                    return false;
                } else {
                    user.setPassword(Utility.getHash(user.getTransPasswordNew1()));
                }
            } else {
                if (!(Utility.isStringEmptyOrNull(user.getTransPasswordNew1())
                        && Utility.isStringEmptyOrNull(user.getTransPasswordNew2()))) {
                    JsfUtil.addValidationFailedForComponent("editForm:new2password", "passwords is diffrent");
                    JsfUtil.failValidation();
                    return false;
                } else {
                    JsfUtil.addValidationFailedForComponent("editForm:new2password", "enter password");
                    JsfUtil.failValidation();
                }
            }
            // user.setPassword(Utility.getHash(user.getTransPassword()));
            user.setUserType(false);
            ejbUser.addUser(user);
            mbLogin.setValue(Constants.VALUE_REGISTERED);
            // JsfUtil.addInfoMessage("Data saved");
            return true;
        }
    }

    public boolean updateUser() {
        //  String passwordMD5 = Utility.getHash(user.getTransPassword());
        if (!Utility.isStringEmptyOrNull(user.getTransPasswordNew1())
                && !Utility.isStringEmptyOrNull((user.getTransPasswordNew2()))) {
            if (!user.getTransPasswordNew1().equals(user.getTransPasswordNew2())) {
                JsfUtil.addValidationFailedForComponent("editForm:new2password", "passwords is diffrent");
                JsfUtil.failValidation();
                return false;
            } else {
                user.setPassword(Utility.getHash(user.getTransPasswordNew1()));
            }
        } else {
            if (!(Utility.isStringEmptyOrNull(user.getTransPasswordNew1())
                    && Utility.isStringEmptyOrNull(user.getTransPasswordNew2()))) {
                JsfUtil.addValidationFailedForComponent("editForm:new2password", "passwords is diffrent");
                JsfUtil.failValidation();
                return false;
            }
        }
        ejbUser.editUser(user);
        mbLogin.setValue(Constants.VALUE_EDIT_USER);
        return true;
    }

    public void deleteUser() {
        ejbUser.deleteUser(user.getIdUserAccess());
        listUser.remove(user);
        JsfUtil.addInfoMessage("User deleted");
    }

    public void changeActiveStatus(UserAccess user) {
        ejbUser.changeActiveStatus(user.getIdUserAccess());
        user.setActive(!user.getActive());
        JsfUtil.addInfoMessage("Status changed.");

    }

    public String navigationToUpdateUser(UserAccess user) {
        System.out.println("before-user-update");
        if (user == null) {
            prepareUserForAdd();
        } else {
            prepareUserForEdit(user);
        }
        return "user-update";
    }

    public String navigationAfterUpdateUser() {
        if (isNewUser()) {
            if (!addUser()) {
                return null;
            }
        } else if (!updateUser()) {
            return null;
        }
        System.out.println("save user");
        return navigationToHome();
    }

    public String navigationToHome() {
        System.out.println("home");
        return "home?faces-redirect=true";
    }

    public String navigationToUserOverview() {
        listUser = ejbUser.getUsers();
        System.out.println("user-overview");
        return "user-overview";
    }

    public UserAccess getUser() {
        return user;
    }

    public void setUser(UserAccess user) {
        this.user = user;
    }

    public List<UserAccess> getListUser() {
        return listUser;
    }

    public MbLogin getMbLogin() {
        return mbLogin;
    }

    public void setMbLogin(MbLogin mbLogin) {
        this.mbLogin = mbLogin;
    }

    public boolean isNewUser() {
        if (user.getIdUserAccess() == 0) {
            return true;
        } else {
            return false;
        }
    }

    public SelectItem[] getActiveOption() {
        return activeOption;
    }

    private SelectItem[] createFilterOptions() {
        SelectItem[] options = new SelectItem[3];
        options[0] = new SelectItem("", "All");
        options[1] = new SelectItem("true", "Active");
        options[2] = new SelectItem("false", "Not active");
        return options;
    }
}
